﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using Web.Helper;

namespace Web.Areas.Manager.Controllers
{
    [UserAuthorize]
    public class ReceiveFormController : Controller
    {
      // ReceiveForm_BLL _ReceiveForm_BLL = new ReceiveForm_BLL();
       Sys_Users_BLL _Sys_Users_BLL = new Sys_Users_BLL();
       Customer_BLL _Customer_BLL = new Customer_BLL();
       ContactOrder_BLL _ContactOrder_BLL = new ContactOrder_BLL();
       SendBill_BLL _SendBill_BLL = new SendBill_BLL();
        private CustomerContact_BLL _CustomerContact_BLL = new CustomerContact_BLL();
        private Quotation_BLL _Quotation_BLL = new Quotation_BLL();
     
       M_ReceiveForm_BLL _M_ReceiveForm_BLL = new M_ReceiveForm_BLL();

          public virtual ActionResult Index(int? pageIndex, string keyword)
        {
            var model = _ContactOrder_BLL.Get().Where(a=>a.State .Equals ("已批准"));
            if (!string.IsNullOrEmpty(keyword))
            {
              //  model = model.Where(a => a.UserName.Contains(keyword));
            }
            return View(model.ToPagedList(pageIndex));
        }


        public virtual ActionResult Details(Guid Id)
        {

            var item = _ContactOrder_BLL.Get(Id);
            return View(item);
        }

        public virtual ActionResult Edit(Guid? Id)
        {
            IList<M_ReceiveForm> list = new List<M_ReceiveForm>();
            ViewData["ReceiveForm"] = list;
            ViewData["Customer"] = _Customer_BLL.Get();
            ViewData["CustomerContact"] = _CustomerContact_BLL.Get();
            ViewData["Quotation"] = _Quotation_BLL.Get();
            ViewData["Sys_Users"] = _Sys_Users_BLL.Get();
            var item = _ContactOrder_BLL.Get(Id);
          
            return View(item);
        }

        [HttpPost]

        public virtual ActionResult Edit(Guid? Id, ContactOrder collection)
        {
            
            IList<M_ReceiveForm> list = new List<M_ReceiveForm>();

            string[] Number = Request["number1"].Split(',');

            string[] ReceivedMoney = Request["ReceivedMoney"].Split(',');
           
           
            string[] Description = Request["Description1"].Split(',');

            for (int i = 0; i < Number.Length; i++)
            {
                M_ReceiveForm item1 = new M_ReceiveForm();
              
                item1.Id = Guid.NewGuid();
                item1.ItemId = collection.Id;
                item1.Number = string.IsNullOrEmpty(Number[i]) ? "" : Number[i];
                
               
               
                item1.ReceivedMoney = decimal.Parse(String.IsNullOrEmpty(ReceivedMoney[i]) || !StringExt.IsPrice(ReceivedMoney[i]) ? "0" : ReceivedMoney[i]);
                item1.DateTime = DateTime.Now;
              
                item1.Description=Description [i];
               
                list.Add(item1);
                
                if (string.IsNullOrEmpty(Number[i]))
                {
                    ModelState.AddModelError("Number", "请填写收款次数编号");
                }
                
                if (string.IsNullOrEmpty(ReceivedMoney[i]) || !StringExt.IsPrice(ReceivedMoney[i]))
                {
                    ModelState.AddModelError("ReceivedMoney", "实收款金额填写不正确");
                }
               
            }
          
            ViewData["ReceiveForm"] = list;

            if (!ModelState.IsValid)
            {
                ViewData["Customer"] = _Customer_BLL.Get();
                ViewData["CustomerContact"] = _CustomerContact_BLL.Get();
                ViewData["Quotation"] = _Quotation_BLL.Get();
                ViewData["Sys_Users"] = _Sys_Users_BLL.Get();
                return View(collection);
            }

            IQueryable<M_ReceiveForm> item3 = _ContactOrder_BLL.Get(Id).M_ReceiveForm.AsQueryable();
            foreach (var mReceiveForm in item3)
            {
                _M_ReceiveForm_BLL.Delete(mReceiveForm.Id);
            }
            
            for (int i = 0; i < Number.Length; i++)
            {
                M_ReceiveForm item1 = new M_ReceiveForm();

                item1.Id = Guid.NewGuid();
                item1.ItemId = collection.Id;
                item1.Number = string.IsNullOrEmpty(Number[i]) ? "" : Number[i];

                item1.ReceivedMoney =
                    decimal.Parse(String.IsNullOrEmpty(ReceivedMoney[i]) || !StringExt.IsPrice(ReceivedMoney[i])
                                      ? "0"
                                      : ReceivedMoney[i]);
                item1.DateTime = DateTime.Now;

                item1.Description = string.IsNullOrEmpty(Description[i]) ? "" : Description[i];
                item1.UserId = _ContactOrder_BLL.Get(Id).AdminId;
                list.Add(item1);
                _M_ReceiveForm_BLL.Save(null,ref item1);
            }
            return RedirectToAction("Index",MyrouteValues.Get());
        }

        [HttpDelete]
        public virtual ActionResult Delete(Guid Id)
        {  
            var item = _ContactOrder_BLL.Get(Id);
           foreach (var item3 in item.M_ReceiveForm)
           {
            _M_ReceiveForm_BLL.Delete(item3.Id);
            }
            return RedirectToAction("Index",MyrouteValues.Get());
        }
    }
}
